(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Introduction to Sigslots

Description: Creating, connecting and triggering signals and slots.

Keywords: ecl sigslots

Tutorial Level: INTERMEDIATE

Next Tutorial: Relaying Signals Debugging Sigslots

Slot Loading

Loading of slots can be done directly to free or member functions through the constructors. Below is a simple example for various types of loading.

   1 void f(const int& i) {}
   2 class A {
   3 public:
   4     void f(const int& i) {}
   5 };
   6 // ...
   7 A a;
   8 Slot<const int&> slot0(f);
   9 Slot<const int&> slot1(&A::f,a);

Connecting

   1 Signal<const int&> signal;
   2 Slot<const int&> slot(f);
   3 signal.connect("Dudes");
   4 slot.connect("Dudes");

Signals and slots have no limit to the number of connections they may make.

Emitting

Every time a signal emits, the connected slots are consecutively run with the data that is emitted.

   1 signal.emit(3); // Pass the '3' to all the slot functions.
   2 

Every emit, the slots are consecutively run - this means that your slots should by nature be short and concise! Otherwise you'll get serious bottlenecks. This is always a good habit to get into for slots otherwise you'll frequently run into problems. If you have a heavy callback, consider spinning that callback off into a thread. That way the thread response will still be quick (cost of a thread creation) and you can still manage heavy workloads.

Design Considerations

  • Keep your slot callbacks concise...failing that, reference them or spin the work off into a thread!
  • For data slots use const references, saves a copy and prevents your original class losing control of its variables.
  • Slots w/ member functions should be member variables of the same class, this guarantees the function is always valid.
  • The sigslots manager may become a bottleneck if you are creating/connecting/disconnecting a large number of slots.
  • If you do need a sigslot implementation that can handle massive numbers of sigslots, fast connection and disconnection, then you probably need to look at the old ecl signals or boost/qt. At the moment, we can't foresee a need for that in

control systems, but if needed, this library can be extended.

Wiki: ecl_sigslots/Tutorials/Introduction to Sigslots (last edited 2012-01-28 08:21:10 by DanielStonier)